package com.colabo.j2ee.web.core.dictionary;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.colabo.j2ee.web.core.exception.ReadableMessage;
import com.colabo.j2ee.web.core.exception.ServiceException;
import com.colabo.j2ee.web.core.pkgenerator.IPKGenerator;
import com.colabo.j2ee.web.core.service.impl.DefaultServiceImpl;
import com.colabo.j2ee.web.core.util.PropertyUtil;
import com.colabo.j2ee.web.core.vo.SessionUserHolder;
import com.colabo.j2ee.web.oaf.common.AppConstants;
import com.colabo.j2ee.web.oaf.monitorperspective.ILogDataService;
import com.colabo.j2ee.web.oaf.vo.IPAuditLogVO;

/**
 * 码表服务实现类�
 */
public class CodeInfoServiceImpl extends DefaultServiceImpl implements ICodeInfoService {
	protected final Log logger = LogFactory.getLog(this.getClass());
	private ILogDataService logDataService = null;

	public IPCodeTypeVO getCodeTypeByTypeCode(String typeCode) throws ServiceException {

		IPCodeTypeVO vo = null;
		List<IPCodeTypeVO> retList = new ArrayList<IPCodeTypeVO>();
		retList = find_iBatis("web.getCodeTypeByTypeCode", typeCode);
		if (retList != null && retList.size() > 0) {
			vo = (IPCodeTypeVO) retList.get(0);
		}
		return vo;
	}

	public String getCodeTypeKyTypeCode(String typeCode) throws ServiceException {
		String ky = null;
		List<String> retList = new ArrayList<String>();
		retList = find_iBatis("web.getCodeTypeKyTypeCode", typeCode);
		if (retList != null && retList.size() > 0) {
			ky = retList.get(0);
		}
		return ky;
	}

	public IPCodeTypeVO getCodeTypeByTypeKy(String typeKy) throws ServiceException {
		return (IPCodeTypeVO) findByID_Hibernate_Get(IPCodeTypeVO.class, typeKy);
	}

	public int getAllCodeTypeCount() throws ServiceException {
		List l = find_iBatis("web.getAllCodeTypeCount", null);
		if (l != null && !l.isEmpty()) {
			return new Integer(l.get(0).toString()).intValue();
		}
		return 0;
	}

	public List getAllCodeTypes(int offset, int pageSize) throws ServiceException {
		List ret = find_iBatis("web.getAllCodeTypes", null, offset, pageSize);
		if (ret == null) {
			return new ArrayList(0);
		}
		return ret;
	}

	public List getAllCodeTypes() throws ServiceException {
		List ret = find_iBatis("web.getAllCodeTypes", null);
		if (ret == null) {
			return new ArrayList(0);
		}
		return ret;
	}
	
	public int getCodeTypeCountByMap(Map para) throws ServiceException {
		List l = find_iBatis("web.getCodeTypeCountByMap", para);
		if (l != null && !l.isEmpty()) {
			return new Integer(l.get(0).toString()).intValue();
		}
		return 0;
	}
	
	public List getCodeTypesByMap(int offset, int pageSize,Map para) throws ServiceException {
		List ret = find_iBatis("web.getCodeTypesByMap", para, offset, pageSize);
		if (ret == null) {
			return new ArrayList(0);
		}
		return ret;
	}

	public void batchCommitCodeType(List insertBeans, List updateBeans, List delBeans) throws ServiceException {
		IPKGenerator keyGen = (IPKGenerator) getBean(IPKGenerator.BEAN_ID);

		SessionUserHolder user = (SessionUserHolder) getBean("sessionUserHolder");
		if (insertBeans != null) {
			int size = insertBeans.size();
			IPCodeTypeVO codeType = null;
			for (int i = 0; i < size; i++) {
				codeType = (IPCodeTypeVO) insertBeans.get(i);
				if (checkCodeTypeExistByCode(codeType.getCodetypeCode())) {
					throw new ServiceException(new ReadableMessage(AppConstants.MSG_ERR_APP_2002, new Object[] { codeType
							.getCodetypeCode() }));
				}
				checkCodeType(codeType);
				codeType.setCodetypeky(keyGen.getUUIDKey(codeType));
				codeType.setVisibleflag("1");
				codeType.setDelflag("n");
				save_Hibernate_Object(codeType);

				logDataService.saveLog(IPAuditLogVO.BIZ_TYPE_SYS_MAINTAIN, IPAuditLogVO.OP_CODE_SAVE,
						user.getStaffId(), user.getLoginIp(), PropertyUtil
								.getPropertyByKey(AppConstants.MSG_INFO_APP_2001)
								+ IPAuditLogVO.SEPARATE
								+ codeType.getCodetypeky()
								+ IPAuditLogVO.SEPARATE
								+ codeType.getCodetypeName());
			}
		}

		if (updateBeans != null) {
			int size = updateBeans.size();
			IPCodeTypeVO codeType = null;
			for (int i = 0; i < size; i++) {
				codeType = (IPCodeTypeVO) updateBeans.get(i);

				if (!(checkCodeTypeCodeExistByKy(codeType.getCodetypeky(), codeType.getCodetypeCode()))) {
					if (checkCodeTypeExistByCode(codeType.getCodetypeCode())) {
						throw new ServiceException(new ReadableMessage(AppConstants.MSG_ERR_APP_2002,
								new Object[] { codeType.getCodetypeCode() }));
					}
				}

				checkCodeType(codeType);
				codeType.setVisibleflag("1");
				codeType.setDelflag("n");
				update_Hibernate_Object(codeType);

				logDataService.saveLog(IPAuditLogVO.BIZ_TYPE_SYS_MAINTAIN, IPAuditLogVO.OP_CODE_UPDATE, user
						.getStaffId(), user.getLoginIp(), PropertyUtil.getPropertyByKey(AppConstants.MSG_INFO_APP_2001)
						+ IPAuditLogVO.SEPARATE + codeType.getCodetypeky() + IPAuditLogVO.SEPARATE
						+ codeType.getCodetypeName());
			}
		}

		if (delBeans != null) {
			int size = delBeans.size();
			IPCodeTypeVO codeType = null;
			for (int i = 0; i < size; i++) {
				codeType = (IPCodeTypeVO) delBeans.get(i);
				codeType.setVisibleflag("1");
				codeType.setDelflag("y");
				deleteCodeInfoByCodeTypeKy(codeType.getCodetypeky());
				update_Hibernate_Object(codeType);

				logDataService.saveLog(IPAuditLogVO.BIZ_TYPE_SYS_MAINTAIN, IPAuditLogVO.OP_CODE_DELETE, user
						.getStaffId(), user.getLoginIp(), PropertyUtil.getPropertyByKey(AppConstants.MSG_INFO_APP_2002)
						+ IPAuditLogVO.SEPARATE + codeType.getCodetypeky() + IPAuditLogVO.SEPARATE
						+ codeType.getCodetypeName());
			}
		}
	}

	private void deleteCodeInfoByCodeTypeKy(String codeTypeKy) throws ServiceException {
		if (codeTypeKy != null) {
			List codeList = getCodeInfoByCodeTypeKy(codeTypeKy);
			if (codeList != null) {
				for (int i = 0; i < codeList.size(); i++) {
					IPCodeInfoVO codeInfo = (IPCodeInfoVO) codeList.get(i);
					if (codeInfo != null) {
						codeInfo.setDelflag("y");
						this.update_Hibernate_Object(codeInfo);
					}
				}
			}

		}
	}

	private void checkCodeType(IPCodeTypeVO codeType) throws ServiceException {
		if (codeType.getCodetypeCode() == null || "".equals(codeType.getCodetypeCode())) {
			throw new ServiceException("");
		}
	}

	public String batchCommitCodeInfo(IPCodeTypeVO codeType, List insertBeans, List updateBeans, List delBeans)
			throws ServiceException {
		if (codeType == null) {
			return null;
		}

		IPKGenerator keyGen = (IPKGenerator) getBean(IPKGenerator.BEAN_ID);
		SessionUserHolder user = (SessionUserHolder) getBean("sessionUserHolder");
		if (insertBeans != null) {
			int count = insertBeans.size();
			for (int i = 0; i < count; i++) {
				IPCodeInfoVO codeInfo = (IPCodeInfoVO) insertBeans.get(i);
				if (checkCodeInfoExistByCode(codeType.getCodetypeCode(), codeInfo.getCodeinfoCode())) {
					return "1" + codeInfo.getCodeinfoCode();

				}
				checkCodeInfo(codeInfo);
				codeInfo.setCodeinfoky(keyGen.getUUIDKey(codeInfo));
				codeInfo.setCodetypeCode(codeType.getCodetypeky());
				if (codeInfo.getVisibleflag() == null) {
					codeInfo.setVisibleflag("1");
				}
				codeInfo.setDelflag("n");
				String parentCode = codeInfo.getParentCode();
				if (parentCode == null || parentCode.trim().equals("")) {
					codeInfo.setCodelevel(Integer.valueOf("1"));
					codeInfo.setRootCode(null);
					codeInfo.setFullpath("," + codeInfo.getCodeinfoCode() + ",");
				} else {
					IPCodeInfoVO parentCodeInfo = (IPCodeInfoVO) this.getCodeInfoVOByInfoCodeAndTypeKy(codeInfo
							.getCodetypeCode(), parentCode);
					if (parentCodeInfo != null) {
						codeInfo.setParentCode(parentCodeInfo.getCodeinfoCode());
						if (parentCodeInfo.getRootCode() == null) {
							codeInfo.setRootCode(parentCodeInfo.getCodeinfoCode());
						} else {
							codeInfo.setRootCode(parentCodeInfo.getRootCode());
						}

						int codeLevel = parentCodeInfo.getCodelevel().intValue() + 1;
						codeInfo.setCodelevel(Integer.valueOf("" + codeLevel));
						codeInfo.setFullpath(parentCodeInfo.getFullpath() + codeInfo.getCodeinfoCode() + ",");
					}
				}
				codeInfo.setParentCodeInfoVO(null);
				save_Hibernate_Object(codeInfo);

				logDataService.saveLog(IPAuditLogVO.BIZ_TYPE_SYS_MAINTAIN, IPAuditLogVO.OP_CODE_SAVE,
						user.getStaffId(), user.getLoginIp(), PropertyUtil
								.getPropertyByKey(AppConstants.MSG_INFO_APP_2002)
								+ IPAuditLogVO.SEPARATE
								+ codeInfo.getCodeinfoky()
								+ IPAuditLogVO.SEPARATE
								+ codeInfo.getCodeinfoDesc());
			}
		}

		if (updateBeans != null) {
			int count = updateBeans.size();
			for (int i = 0; i < count; i++) {
				IPCodeInfoVO codeInfo = (IPCodeInfoVO) updateBeans.get(i);
				if (codeInfo.getCodeinfoky() != null && codeInfo.getCodeinfoky().length() > 0) {
					if (!(checkCodeInfoCodeExistByKy(codeInfo.getCodeinfoky(), codeInfo.getCodeinfoCode()))) {
						if (checkCodeInfoExistByCode(codeType.getCodetypeCode(), codeInfo.getCodeinfoCode())) {
							return "1" + codeInfo.getCodeinfoCode();
						}
					}

					checkCodeInfo(codeInfo);
					if (codeInfo.getVisibleflag() == null) {
						codeInfo.setVisibleflag("1");
					}
					codeInfo.setDelflag("n");
					String parentCode = codeInfo.getParentCode();
					boolean isChange = checkParentCodeInfoChange(codeInfo.getCodeinfoky(), codeInfo.getParentCode());
					Integer level = codeInfo.getCodelevel();
					if (isChange) {
						if (parentCode == null || parentCode.trim().equals("")) {
							codeInfo.setCodelevel(Integer.valueOf("1"));
							codeInfo.setRootCode(null);
							codeInfo.setFullpath("," + codeInfo.getCodeinfoCode() + ",");
						} else {
							IPCodeInfoVO parentCodeInfo = (IPCodeInfoVO) this.getCodeInfoVOByInfoCodeAndTypeKy(codeInfo
									.getCodetypeCode(), parentCode);
							if (parentCodeInfo != null) {
								codeInfo.setParentCode(parentCodeInfo.getCodeinfoCode());
								if (parentCodeInfo.getRootCode() == null) {
									codeInfo.setRootCode(parentCodeInfo.getCodeinfoCode());
								} else {
									codeInfo.setRootCode(parentCodeInfo.getRootCode());
								}
								int codeLevel = parentCodeInfo.getCodelevel().intValue() + 1;
								codeInfo.setCodelevel(Integer.valueOf("" + codeLevel));
								codeInfo.setFullpath(parentCodeInfo.getFullpath() + codeInfo.getCodeinfoCode() + ",");
							}
						}

						this.updateChildCodeInfo(codeInfo, level);
					} else {
						if (parentCode == null || parentCode.trim().equals("")) {
							codeInfo.setCodelevel(Integer.valueOf("1"));
							codeInfo.setRootCode(null);
							codeInfo.setFullpath("," + codeInfo.getCodeinfoCode() + ",");
						} else {
							IPCodeInfoVO parentCodeInfo = (IPCodeInfoVO) this.getCodeInfoVOByInfoCodeAndTypeKy(codeInfo
									.getCodetypeCode(), parentCode);
							if (parentCodeInfo != null) {
								codeInfo.setParentCode(parentCodeInfo.getCodeinfoCode());
								if (parentCodeInfo.getRootCode() == null) {
									codeInfo.setRootCode(parentCodeInfo.getCodeinfoCode());
								} else {
									codeInfo.setRootCode(parentCodeInfo.getRootCode());
								}
								int codeLevel = parentCodeInfo.getCodelevel().intValue() + 1;
								codeInfo.setCodelevel(Integer.valueOf("" + codeLevel));
								codeInfo.setFullpath(parentCodeInfo.getFullpath() + codeInfo.getCodeinfoCode() + ",");
							}
						}
					}
					codeInfo.setParentCodeInfoVO(null);
					update_Hibernate_Object(codeInfo);

					logDataService.saveLog(IPAuditLogVO.BIZ_TYPE_SYS_MAINTAIN, IPAuditLogVO.OP_CODE_UPDATE, user
							.getStaffId(), user.getLoginIp(), PropertyUtil
							.getPropertyByKey(AppConstants.MSG_INFO_APP_2002)
							+ IPAuditLogVO.SEPARATE
							+ codeInfo.getCodeinfoky()
							+ IPAuditLogVO.SEPARATE
							+ codeInfo.getCodeinfoDesc());
				} else {
					if (checkCodeInfoExistByCode(codeType.getCodetypeCode(), codeInfo.getCodeinfoCode())) {
						return "1" + codeInfo.getCodeinfoCode();
					}
					checkCodeInfo(codeInfo);
					codeInfo.setCodeinfoky(keyGen.getUUIDKey(codeInfo));
					codeInfo.setCodetypeCode(codeType.getCodetypeky());
					if (codeInfo.getVisibleflag() == null) {
						codeInfo.setVisibleflag("1");
					}
					codeInfo.setDelflag("n");
					String parentCode = codeInfo.getParentCode();
					if (parentCode == null || parentCode.trim().equals("")) {
						codeInfo.setCodelevel(Integer.valueOf("1"));
						codeInfo.setRootCode(null);
						codeInfo.setFullpath("," + codeInfo.getCodeinfoCode() + ",");
					} else {
						IPCodeInfoVO parentCodeInfo = (IPCodeInfoVO) this.getCodeInfoVOByInfoCodeAndTypeKy(codeInfo
								.getCodetypeCode(), parentCode);
						if (parentCodeInfo != null) {
							codeInfo.setParentCode(parentCodeInfo.getCodeinfoCode());
							if (parentCodeInfo.getRootCode() == null) {
								codeInfo.setRootCode(parentCodeInfo.getCodeinfoCode());
							} else {
								codeInfo.setRootCode(parentCodeInfo.getRootCode());
							}

							int codeLevel = parentCodeInfo.getCodelevel().intValue() + 1;
							codeInfo.setCodelevel(Integer.valueOf("" + codeLevel));
							codeInfo.setFullpath(parentCodeInfo.getFullpath() + codeInfo.getCodeinfoCode() + ",");
						}
					}
					codeInfo.setParentCodeInfoVO(null);
					save_Hibernate_Object(codeInfo);

				}
			}
		}

		if (delBeans != null) {
			int count = delBeans.size();
			for (int i = 0; i < count; i++) {
				IPCodeInfoVO codeInfo = (IPCodeInfoVO) delBeans.get(i);
				if (codeInfo.getCodeinfoky() != null && codeInfo.getCodeinfoky().length() > 0) {
					if (codeInfo.getVisibleflag() == null) {
						codeInfo.setVisibleflag("1");
					}
					codeInfo.setDelflag("y");
					codeInfo.setParentCodeInfoVO(null);
					deleteChildCodeInfo(codeInfo);
					update_Hibernate_Object(codeInfo);
				}
				logDataService.saveLog(IPAuditLogVO.BIZ_TYPE_SYS_MAINTAIN, IPAuditLogVO.OP_CODE_DELETE, user
						.getStaffId(), user.getLoginIp(), PropertyUtil.getPropertyByKey(AppConstants.MSG_INFO_APP_2002)
						+ IPAuditLogVO.SEPARATE + codeInfo.getCodeinfoky() + IPAuditLogVO.SEPARATE
						+ codeInfo.getCodeinfoDesc());
			}
		}
		return "0";
	}

	private void deleteChildCodeInfo(IPCodeInfoVO parentVO) throws ServiceException {

		if (parentVO != null) {
			List childs = this.getChildCodeInfoListByKy(parentVO.getCodetypeCode(), parentVO.getCodeinfoCode());
			if (childs != null) {
				for (int i = 0; i < childs.size(); i++) {
					IPCodeInfoVO codeInfo = (IPCodeInfoVO) childs.get(i);
					if (codeInfo != null) {
						codeInfo.setDelflag("y");
						this.update_Hibernate_Object(codeInfo);

					}
				}
			}
		}

	}

	private void checkCodeInfo(IPCodeInfoVO codeInfo) {
	}

	public List getCodeInfoByCodeType(String codeType) throws ServiceException {

		List list = new ArrayList();

		String codeTypeKy = this.getCodeTypeKyTypeCode(codeType);
		if(codeTypeKy!=null){
			list = getCodeInfoByCodeTypeKy(codeTypeKy);
		}
		return list;
	}

	private List getCodeInfoByCodeTypeKy(String codeTypeKy) throws ServiceException {
		List list = new ArrayList();

		List ret = find_iBatis("web.getCodeInfoByCodeType", codeTypeKy);
		if (ret == null) {
			return new ArrayList(0);
		}

		if (ret != null) {
			for (int i = 0; i < ret.size(); i++) {
				IPCodeInfoVO vo = (IPCodeInfoVO) ret.get(i);
				if (vo != null) {

					IPCodeTypeVO typeVO = getCodeTypeByTypeKy(vo.getCodetypeCode());
					IPCodeInfoVO parentVO = getCodeInfoVOByInfoCodeAndTypeKy(vo.getCodetypeCode(), vo.getParentCode());

					vo.setCodeTypeVO(typeVO);
					vo.setParentCodeInfoVO(parentVO);
					list.add(vo);
				}
			}
		}

		return list;
	}

	private boolean checkCodeTypeCodeExistByKy(String codeTypeKy, String codeTypeCode) throws ServiceException {
		Map paramMap = new HashMap();
		paramMap.put("codetypeky", codeTypeKy);
		paramMap.put("codetypecode", codeTypeCode);
		List l = find_iBatis("web.checkCodeTypeCodeExistByKy", paramMap);
		if (l != null && !l.isEmpty()) {
			Integer cnt = ((Integer) l.get(0));
			if (cnt != null && cnt.intValue() > 0) {
				return true;
			}
		}
		return false;
	}

	private boolean checkCodeTypeExistByCode(String codeTypeCode) throws ServiceException {
		Map paramMap = new HashMap();
		paramMap.put("codetypecode", codeTypeCode);
		List l = find_iBatis("web.checkCodeTypeExistByCode", paramMap);
		if (l != null && !l.isEmpty()) {
			Integer cnt = ((Integer) l.get(0));
			if (cnt != null && cnt.intValue() > 0) {
				return true;
			}
		}
		return false;
	}

	private boolean checkCodeInfoCodeExistByKy(String codeInfoKy, String codeInfoCode) throws ServiceException {
		Map paramMap = new HashMap();
		paramMap.put("codeinfoky", codeInfoKy);
		paramMap.put("codeinfocode", codeInfoCode);
		List l = find_iBatis("web.checkCodeInfoCodeExistByKy", paramMap);
		if (l != null && !l.isEmpty()) {
			Integer cnt = ((Integer) l.get(0));
			if (cnt != null && cnt.intValue() > 0) {
				return true;
			}
		}
		return false;
	}

	private boolean checkCodeInfoExistByCode(String codeType, String codeInfoCode) throws ServiceException {
		Map paramMap = new HashMap();
		String codeTypeKy = this.getCodeTypeKyTypeCode(codeType);
		paramMap.put("codeinfotype", codeTypeKy);
		paramMap.put("codeinfocode", codeInfoCode);
		List l = find_iBatis("web.checkCodeInfoExistByCode", paramMap);
		if (l != null && !l.isEmpty()) {
			Integer cnt = ((Integer) l.get(0));
			if (cnt != null && cnt.intValue() > 0) {
				return true;
			}
		}
		return false;
	}

	private boolean checkParentCodeInfoChange(String codeInfoKy, String parentCode) throws ServiceException {
		Map paramMap = new HashMap();
		paramMap.put("codeinfoky", codeInfoKy);
		paramMap.put("parentcode", parentCode);
		List l = find_iBatis("web.checkParentCodeInfoChange", paramMap);
		if (l != null && !l.isEmpty()) {
			Integer cnt = ((Integer) l.get(0));
			if (cnt != null && cnt.intValue() > 0) {
				return false;
			}
		}
		return true;
	}

	/**
	 * 查询出code表的所有子节点
	 */
	public List getChildCodeInfoList(String codeType, String codeInfoCode) throws ServiceException {
		List list = new ArrayList();
		String codeTypeKy = this.getCodeTypeKyTypeCode(codeType);
		list = getChildCodeInfoListByKy(codeTypeKy, codeInfoCode);

		return list;

	}

	public List getChildCodeInfoListByKy(String codeTypeKy, String codeInfoCode) throws ServiceException {
		List list = new ArrayList();

		Map paramMap = new HashMap();
		paramMap.put("codetypeky", codeTypeKy);
		paramMap.put("codeinfocode", "," + codeInfoCode + ",");
		List ret = find_iBatis("web.getChildCodeInfoByCode", paramMap);

		if (ret != null) {
			for (int i = 0; i < ret.size(); i++) {
				IPCodeInfoVO vo = (IPCodeInfoVO) ret.get(i);
				if (vo != null) {

					IPCodeTypeVO typeVO = getCodeTypeByTypeKy(vo.getCodetypeCode());
					IPCodeInfoVO parentVO = getCodeInfoVOByInfoCodeAndTypeKy(vo.getCodetypeCode(), vo.getParentCode());

					vo.setCodeTypeVO(typeVO);
					vo.setParentCodeInfoVO(parentVO);
					list.add(vo);
				}
			}
		}

		return list;
	}

	/**
	 * 更新所有子节点的信息
	 * 
	 * @param parentVO
	 *            父对象
	 * @param parentOldLevel
	 *            父对象级别的原值
	 * @throws ServiceException
	 */
	private void updateChildCodeInfo(IPCodeInfoVO parentVO, Integer parentOldLevel) throws ServiceException {
		if (parentVO != null) {
			List childs = this.getChildCodeInfoListByKy(parentVO.getCodetypeCode(), parentVO.getCodeinfoCode());
			if (childs != null) {
				for (int i = 0; i < childs.size(); i++) {
					IPCodeInfoVO codeInfo = (IPCodeInfoVO) childs.get(i);
					if (codeInfo != null) {
						String root = parentVO.getRootCode();
						if (root != null) {
							codeInfo.setRootCode(root);

						} else {
							codeInfo.setRootCode(parentVO.getCodeinfoCode());
						}

						String fullpath = codeInfo.getFullpath();

						String parentFullPath = parentVO.getFullpath();

						String subFullpath = fullpath.substring(fullpath
								.indexOf("," + parentVO.getCodeinfoCode() + ",")
								+ parentVO.getCodeinfoCode().length() + 2);

						codeInfo.setFullpath(parentFullPath + subFullpath);

						Integer level = codeInfo.getCodelevel();
						Integer parentLevel = parentVO.getCodelevel();

						int codeLevel = level.intValue() + parentLevel.intValue() - parentOldLevel.intValue();
						codeInfo.setCodelevel(Integer.valueOf("" + codeLevel));

						this.update_Hibernate_Object(codeInfo);

					}
				}
			}
		}

	}

	public List getCodeInfoByCodeTypeKy(String codeTypeKy, int offset, int pageSize) throws ServiceException {
		List list = new ArrayList();

		List ret = find_iBatis("web.getCodeInfoByCodeTypeForPage", codeTypeKy, offset, pageSize);
		if (ret == null) {
			return new ArrayList(0);
		}

		if (ret != null) {
			for (int i = 0; i < ret.size(); i++) {
				IPCodeInfoVO vo = (IPCodeInfoVO) ret.get(i);
				if (vo != null) {

					IPCodeTypeVO typeVO = getCodeTypeByTypeKy(vo.getCodetypeCode());
					IPCodeInfoVO parentVO = getCodeInfoVOByInfoCodeAndTypeKy(vo.getCodetypeCode(), vo.getParentCode());

					vo.setCodeTypeVO(typeVO);
					vo.setParentCodeInfoVO(parentVO);
					list.add(vo);
				}
			}
		}

		return list;
	}

	public int getCodeInfoCountByCodeTypeKyForPage(String codeType) throws ServiceException {
		List ret = find_iBatis("web.getCodeInfoCountByCodeTypeForPage", codeType);
		if (ret == null || ret.size() <= 0) {
			return 0;
		}

		Integer cnt = ((Integer) ret.get(0));
		return cnt.intValue();
	}

	public int getCodeInfoCountByCodeTypeKy(String codeType) throws ServiceException {
		List ret = find_iBatis("web.getCodeInfoCountByCodeType", codeType);
		if (ret == null || ret.size() <= 0) {
			return 0;
		}

		Integer cnt = ((Integer) ret.get(0));
		return cnt.intValue();
	}

	public IPCodeInfoVO getCodeInfoVOByInfoCode(String codeTypeCode, String codeinfoCode) throws ServiceException {
		if (codeinfoCode == null) {
			return null;
		}
		String codeTypeKy = this.getCodeTypeKyTypeCode(codeTypeCode);
		if (codeTypeKy == null || codeTypeKy.length() < 1) {
			return null;
		}
		IPCodeInfoVO vo = null;
		List retList = new ArrayList();
		Map paramMap = new HashMap();
		paramMap.put("codetypecode", codeTypeKy);
		paramMap.put("codeinfocode", codeinfoCode);
		retList = find_iBatis("web.getCodeInfoVOByInfoCode", paramMap);
		if (retList != null && retList.size() > 0) {
			vo = (IPCodeInfoVO) retList.get(0);
		}

		if (vo != null) {
			IPCodeTypeVO typeVO = getCodeTypeByTypeKy(vo.getCodetypeCode());
			IPCodeInfoVO parentVO = getCodeInfoVOByInfoCodeAndTypeKy(vo.getCodetypeCode(), vo.getParentCode());

			vo.setCodeTypeVO(typeVO);
			vo.setParentCodeInfoVO(parentVO);
		}

		return vo;
	}

	public IPCodeInfoVO getCodeInfoVOByInfoCodeAndTypeKy(String codeTypeKy, String codeinfoCode)
			throws ServiceException {
		if (codeinfoCode == null) {
			return null;
		}

		IPCodeInfoVO vo = null;
		List retList = new ArrayList();
		Map paramMap = new HashMap();
		paramMap.put("codetypecode", codeTypeKy);
		paramMap.put("codeinfocode", codeinfoCode);
		retList = find_iBatis("web.getCodeInfoVOByInfoCode", paramMap);
		if (retList != null && retList.size() > 0) {
			vo = (IPCodeInfoVO) retList.get(0);
		}
		if (vo != null) {
			IPCodeTypeVO typeVO = getCodeTypeByTypeKy(vo.getCodetypeCode());
			IPCodeInfoVO parentVO = getCodeInfoVOByInfoCodeAndTypeKy(vo.getCodetypeCode(), vo.getParentCode());

			vo.setCodeTypeVO(typeVO);
			vo.setParentCodeInfoVO(parentVO);
		}

		return vo;
	}

	public IPCodeInfoVO getCodeInfoVOByKy(String codeinfoky) throws ServiceException {
		if (codeinfoky == null) {
			return null;
		}

		IPCodeInfoVO codevo = (IPCodeInfoVO) findByID_Hibernate_Get(IPCodeInfoVO.class, codeinfoky);
		IPCodeTypeVO typeVO = getCodeTypeByTypeKy(codevo.getCodetypeCode());
		IPCodeInfoVO parentVO = getCodeInfoVOByInfoCodeAndTypeKy(codevo.getCodetypeCode(), codevo.getParentCode());

		codevo.setCodeTypeVO(typeVO);
		codevo.setParentCodeInfoVO(parentVO);
		return codevo;
	}

	public IPCodeTypeVO getCodeTypeVOByTypeCode(String codeType) throws ServiceException {
		if (codeType == null) {
			return null;
		}

		return this.getCodeTypeByTypeCode(codeType);
	}

	public List getAllParentCodeInfoList(String codetypeCode, String codeInfoCode) throws ServiceException {
		List list = new ArrayList();
		String codeTypeKy = this.getCodeTypeKyTypeCode(codetypeCode);
		IPCodeInfoVO vo = this.getCodeInfoVOByInfoCodeAndTypeKy(codeTypeKy, codeInfoCode);
		if (vo != null && vo.getParentCode() != null) {
			String fullPath = vo.getFullpath();
			if (fullPath != null && fullPath.length() > 1) {
				fullPath = fullPath.substring(1, fullPath.lastIndexOf(","));

				if (fullPath != null && fullPath.indexOf(",") > 0) {
					fullPath = fullPath.substring(0, fullPath.indexOf(codeInfoCode) + 1);
					String[] parentCodeArray = fullPath.split(",");
					if (parentCodeArray != null) {
						for (int i = 0; i < parentCodeArray.length; i++) {
							String parentCode = parentCodeArray[i];
							if (parentCode != null) {
								IPCodeInfoVO tmp = this.getCodeInfoVOByInfoCode(codetypeCode, parentCode);
								if (tmp != null) {

									IPCodeTypeVO typeVO = getCodeTypeByTypeKy(tmp.getCodetypeCode());
									IPCodeInfoVO parentVO = getCodeInfoVOByInfoCodeAndTypeKy(tmp.getCodetypeCode(), tmp
											.getParentCode());

									tmp.setCodeTypeVO(typeVO);
									tmp.setParentCodeInfoVO(parentVO);
									list.add(tmp);
									// retList.add(tmp);
								}
							}
						}
					}
				}

			}
		}

		return list;
	}

	public List getChildCodeInfoByLevel(String codetypeCode, String codeInfoCode, int fromLev, int toLev)
			throws ServiceException {
		String codeTypeKy = this.getCodeTypeKyTypeCode(codetypeCode);
		IPCodeInfoVO vo = this.getCodeInfoVOByInfoCodeAndTypeKy(codeTypeKy, codeInfoCode);
		List retList = new ArrayList();
		List list = new ArrayList();

		if (fromLev <= 0 || toLev <= 0 || toLev < fromLev) {
			return retList;
		}

		if (vo != null) {
			int level = vo.getCodelevel().intValue();
			int endLevel = level + toLev - fromLev + 1;

			Map paramMap = new HashMap();
			paramMap.put("codetypecode", codeTypeKy);
			paramMap.put("codeinfocode", "," + codeInfoCode + ",");
			paramMap.put("level", "" + level);
			paramMap.put("endLevel", "" + endLevel);
			retList = find_iBatis("web.getChildCodeInfoByLevel", paramMap);
		}

		if (retList != null) {
			for (int i = 0; i < retList.size(); i++) {
				IPCodeInfoVO codevo = (IPCodeInfoVO) retList.get(i);
				if (vo != null) {

					IPCodeTypeVO typeVO = getCodeTypeByTypeKy(codevo.getCodetypeCode());
					IPCodeInfoVO parentVO = getCodeInfoVOByInfoCodeAndTypeKy(codevo.getCodetypeCode(), codevo
							.getParentCode());

					codevo.setCodeTypeVO(typeVO);
					codevo.setParentCodeInfoVO(parentVO);
					list.add(codevo);
				}
			}
		}

		return list;
	}

	public List getOneLevelChildCodeInfoList(String codetypeCode, String codeInfoCode) throws ServiceException {
		List list = new ArrayList();
		String codeTypeKy = this.getCodeTypeKyTypeCode(codetypeCode);
		Map paramMap = new HashMap();
		paramMap.put("codetypecode", codeTypeKy);
		paramMap.put("codeinfocode", codeInfoCode);
		List retList = find_iBatis("web.getOneLevelChildCodeInfoList", paramMap);

		if (retList != null) {
			for (int i = 0; i < retList.size(); i++) {
				IPCodeInfoVO vo = (IPCodeInfoVO) retList.get(i);
				if (vo != null) {

					IPCodeTypeVO typeVO = getCodeTypeByTypeKy(vo.getCodetypeCode());
					IPCodeInfoVO parentVO = getCodeInfoVOByInfoCodeAndTypeKy(vo.getCodetypeCode(), vo.getParentCode());

					vo.setCodeTypeVO(typeVO);
					vo.setParentCodeInfoVO(parentVO);
					list.add(vo);
				}
			}
		}

		return list;
	}

	public IPCodeInfoVO getParentCodeInfo(String codetypeCode, String codeInfoCode) throws ServiceException {
		IPCodeInfoVO vo = this.getCodeInfoVOByInfoCode(codetypeCode, codeInfoCode);
		if (vo == null || vo.getParentCode() == null) {
			return null;
		} else {
			return getCodeInfoVOByInfoCodeAndTypeKy(vo.getCodetypeCode(), vo.getParentCode());
		}
	}


	public List getFirstLevelCodeInfoByCodeType(String codeType) throws ServiceException {
		List list = new ArrayList();
		String codeTypeKy = this.getCodeTypeKyTypeCode(codeType);
		Map paramMap = new HashMap();
		paramMap.put("codetypecode", codeTypeKy);
		List retList = find_iBatis("web.getFirstLevelCodeInfoByCodeType", paramMap);

		if (retList != null) {
			for (int i = 0; i < retList.size(); i++) {
				IPCodeInfoVO vo = (IPCodeInfoVO) retList.get(i);
				if (vo != null) {

					IPCodeTypeVO typeVO = getCodeTypeByTypeKy(vo.getCodetypeCode());
					IPCodeInfoVO parentVO = getCodeInfoVOByInfoCodeAndTypeKy(vo.getCodetypeCode(), vo.getParentCode());

					vo.setCodeTypeVO(typeVO);
					vo.setParentCodeInfoVO(parentVO);
					list.add(vo);
				}
			}
		}

		return list;
	}

	public List selectParentCodeByCodeTypeKy(String codeTypeKy, String codeinfoCode, int offset, int pageSize)
			throws ServiceException {
		List list = new ArrayList();

		Map map = new HashMap();
		map.put("codetype", codeTypeKy);
		map.put("codeinfocode", "," + codeinfoCode + ",");
		List ret = find_iBatis("web.selectParentCodeByCodeType", map, offset, pageSize);
		if (ret == null) {
			return new ArrayList(0);
		}

		if (ret != null) {
			for (int i = 0; i < ret.size(); i++) {
				IPCodeInfoVO vo = (IPCodeInfoVO) ret.get(i);
				if (vo != null) {

					IPCodeTypeVO typeVO = getCodeTypeByTypeKy(vo.getCodetypeCode());
					IPCodeInfoVO parentVO = getCodeInfoVOByInfoCodeAndTypeKy(vo.getCodetypeCode(), vo.getParentCode());

					vo.setCodeTypeVO(typeVO);
					vo.setParentCodeInfoVO(parentVO);
					list.add(vo);
				}
			}
		}

		return list;
	}

	public int selectCodeCountByCodeTypeKy(String codeTypeKy, String codeinfoCode) throws ServiceException {
		Map map = new HashMap();
		map.put("codetype", codeTypeKy);
		map.put("codeinfocode", "," + codeinfoCode + ",");
		List ret = find_iBatis("web.selectParentCodeCountByCodeType", map);
		if (ret == null || ret.size() <= 0) {
			return 0;
		}

		Integer cnt = ((Integer) ret.get(0));
		return cnt.intValue();
	}

	public List getAllCodeInfo() throws ServiceException {
		List list = new ArrayList();
		List ret = find_iBatis("web.selectAllCodeInfo", new HashMap());
		if (ret == null) {
			return new ArrayList(0);
		}

		boolean isInformix = false;
		if(this.getDatabaseType().equals("informix")){
			isInformix = true;
		}
		if (ret != null) {
			for (int i = 0; i < ret.size(); i++) {
				HashMap hm = (HashMap) ret.get(i);
				if (hm != null) {

					IPCodeInfoVO vo = new IPCodeInfoVO();
					String codeinfoky;
					String codeinfoCode;
					String codeinfoValue;
					String codeinfoDesc;
					String locale;
					Integer indexNo;
					String codetypeCode;
					String delflag;
					String visibleflag;
					String rootCode;
					String parentCode;
					Integer codelevel;
					String fullpath;
					String codetypeKy;
					String ext1;
					// IPCodeTypeVO typeVO =
					// this.getCodeTypeByTypeKy(codetypeCode);
					// IPCodeInfoVO parentVO =
					// getCodeInfoVOByInfoCodeAndTypeKy(typeVO.getCodetypeCode(),
					// parentCode);
					if (isInformix) {
						codeinfoky = (String) hm.get("codeinfoky");
						codeinfoCode = (String) hm.get("codeinfo_code");
						codeinfoValue = (String) hm.get("codeinfo_value");
						codeinfoDesc = (String) hm.get("codeinfo_desc");
						locale = (String) hm.get("locale");
						Object index = hm.get("index_no");
						indexNo = Integer.valueOf("0");
						if (index != null) {
							indexNo = Integer.valueOf(index.toString());
						}
						codetypeCode = (String) hm.get("codetype_code");
						delflag = (String) hm.get("delflag");
						visibleflag = (String) hm.get("visibleflag");
						rootCode = (String) hm.get("root_code");
						parentCode = (String) hm.get("parent_code");
						Object bigLevel = (Object) hm.get("codelevel");
						codelevel = Integer.valueOf(bigLevel == null ? "0" : bigLevel.toString());
						fullpath = (String) hm.get("fullpath");
						codetypeKy = (String) hm.get("codetypeky");
						ext1 = (String) hm.get("ext1");
					} else {
						codeinfoky = (String) hm.get("CODEINFOKY");
						codeinfoCode = (String) hm.get("CODEINFO_CODE");
						codeinfoValue = (String) hm.get("CODEINFO_VALUE");
						codeinfoDesc = (String) hm.get("CODEINFO_DESC");
						locale = (String) hm.get("LOCALE");
						Object index = hm.get("INDEX_NO");
						indexNo = Integer.valueOf("0");
						if (index != null) {
							indexNo = Integer.valueOf(index.toString());
						}
						codetypeCode = (String) hm.get("CODETYPE_CODE");
						delflag = (String) hm.get("DELFLAG");
						visibleflag = (String) hm.get("VISIBLEFLAG");
						rootCode = (String) hm.get("ROOT_CODE");
						parentCode = (String) hm.get("PARENT_CODE");
						Object bigLevel = (Object) hm.get("CODELEVEL");
						codelevel = Integer.valueOf(bigLevel == null ? "0" : bigLevel.toString());
						fullpath = (String) hm.get("FULLPATH");
						codetypeKy = (String) hm.get("CODETYPEKY");
						ext1 = (String) hm.get("EXT1");
					}

					// vo.setCodeTypeVO(typeVO);
					// vo.setParentCodeInfoVO(parentVO);

					vo.setCodeinfoky(codeinfoky);
					vo.setCodeinfoCode(codeinfoCode);
					vo.setCodeinfoValue(codeinfoValue);
					vo.setCodeinfoDesc(codeinfoDesc);
					vo.setCodetypeCode(codetypeKy);
					vo.setLocale(locale);
					vo.setIndexNo(indexNo);
					vo.setDelflag(delflag);
					vo.setVisibleflag(visibleflag);
					vo.setRootCode(rootCode);
					vo.setParentCode(parentCode);
					vo.setCodelevel(codelevel);
					vo.setFullpath(fullpath);
					vo.setExt1(ext1);
					IPCodeTypeVO typeVO = new IPCodeTypeVO();
					typeVO.setCodetypeky(codetypeKy);
					typeVO.setCodetypeCode(codetypeCode);
					vo.setCodeTypeVO(typeVO);
					list.add(vo);
				}
			}
		}

		return list;
	}
	
	public String getDatabaseType() throws ServiceException {
		String dbName = "";
		Connection conn = null;
		try {

			conn = getJdbcTemplate().getDataSource().getConnection();
			String driverName = conn.getMetaData().getDriverName();
			if (driverName.indexOf("oracle") != -1) {
				dbName = "oracle";
			} else if (driverName.indexOf("informix") != -1) {
				dbName = "informix";
			} else if (driverName.indexOf("sqlserver") != -1) {
				dbName = "sqlserver";
			} else if (driverName.indexOf("sybase") != -1) {
				dbName = "sybase";
			} else if (driverName.indexOf("db2") != -1) {
				dbName = "db2";
			} else if (driverName.indexOf("mysql") != -1) {
				dbName = "mysql";
			} else {
				dbName = "oracle";
			}

		} catch (ServiceException e) {

			e.printStackTrace();
		} catch (SQLException e) {

			e.printStackTrace();
		} finally {
			try {
				if (conn != null) {
					conn.close();
				}
			} catch (SQLException e) {
				e.printStackTrace();
				throw new ServiceException(e);
			}
		}

		return dbName;
	}

	public ILogDataService getLogDataService() {
		return logDataService;
	}

	public void setLogDataService(ILogDataService logDataService) {
		this.logDataService = logDataService;
	}
}
